/**
 * 
 */
package com.qjs.smterp.model.primefaces;

import java.util.Comparator;

import org.primefaces.model.SortOrder;

import com.qjs.smterp.model.Instock;

/**
 * @author tudd
 * 
 */
public class LazySorter implements Comparator<Instock> {
	private String sortField;
	private SortOrder sortOrder;

	public LazySorter(String sortField, SortOrder sortOrder) {
		this.sortField = sortField;
		this.sortOrder = sortOrder;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int compare(Instock instock1, Instock instock2) {
		// TODO Auto-generated method stub
		try {
			Object value1 = Instock.class.getField(this.sortField)
					.get(instock1);
			Object value2 = Instock.class.getField(this.sortField)
					.get(instock2);

			int value = ((Comparable) value1).compareTo(value2);

			return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;

		} catch (Exception e) {
			throw new RuntimeException();
		}
	}

}
